package com.itlong.cloud.operate.service.impl;


import com.itlong.cloud.POJO.DTO.operate.OperateOurPayInfoDTO;
import com.itlong.cloud.POJO.DTO.operate.OperateThirdPayInfoDTO;
import com.itlong.cloud.POJO.DTO.operate.OperateThirdPayPageDTO;
import com.itlong.cloud.POJO.PO.ThirdPartyPaymentManagementInfoPO;
import com.itlong.cloud.POJO.VO.operate.OperateOurThirdPayVO;
import com.itlong.cloud.POJO.VO.operate.OperateThirdPayVO;
import com.itlong.cloud.POJO.VO.property.PropertyProjectAmountVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.commons.chinapay.service.IChinaPayTransferAccountService;
import com.itlong.cloud.enumerate.DataStatusEnum;
import com.itlong.cloud.operate.dao.IOperateThirdPayDao;
import com.itlong.cloud.operate.service.IOperateThirdPayService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.random.LogicIdUtil;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      运营-财务管理-第三方支付管理接口实现类
 * </desc>
 *
 * @createDate 2018/09/25
 */
@Service
public class OperateThirdPayServiceImpl implements IOperateThirdPayService {

    private static final Logger LOG = LoggerFactory.getLogger(OperateThirdPayServiceImpl.class);

    @Autowired
    IOperateThirdPayDao iOperateThirdPayDao;

    @Autowired
    IChinaPayTransferAccountService iChinaPayTransferAccountService;

    /**
     * <desc>
     *      第三方支付信息分页查询
     * <desc/>
     *
     * @param operateThirdPayPageDTO 分页查询参数
     * @return
     * @author Qiang.S
     * @createDate 2018-09-25
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<OperateThirdPayVO> getThirdPayInfoPage(OperateThirdPayPageDTO operateThirdPayPageDTO) throws Exception {
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        decimalFormat.setRoundingMode(RoundingMode.HALF_UP);
        //将DTO转换为Map params
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(operateThirdPayPageDTO);
        //分页类默认pageSize为20
        Page<OperateThirdPayVO> page=new Page<>();
        page.getPage().setPageSize(operateThirdPayPageDTO.getPageSize());
        page.getPage().setCurrentPage(operateThirdPayPageDTO.getCurrentPage());
        params.put("projectId",operateThirdPayPageDTO.getProjectId());
        params.put("currentPage",page.getPage().getStartSlip());
        params.put("pageSize",page.getPage().getPageSize());
        //填充返回数据集
        List<OperateThirdPayVO> list = iOperateThirdPayDao.getThirdPayInfoPage(params);
        for (OperateThirdPayVO operateThirdPayVO:list) {
            if ("2".equals(operateThirdPayVO.getThirdPartyPayment())) {//银联
                String amount = iChinaPayTransferAccountService.getAccountBalance();
                operateThirdPayVO.setAccountAmount(decimalFormat.format(new BigDecimal(amount).divide(new BigDecimal("100"))));
                operateThirdPayVO.setAllowedCashWithdrawl(decimalFormat.format(new BigDecimal(amount).divide(new BigDecimal("100"))));
                LOG.info("查询银联备付金余额："+operateThirdPayVO.getAccountAmount());
            }else if ("1".equals(operateThirdPayVO.getThirdPartyPayment())) {//微信
                PropertyProjectAmountVO propertyProjectAmountVO = iOperateThirdPayDao.getProjectAmountSum();
                if (propertyProjectAmountVO != null) {
                    operateThirdPayVO.setAccountAmount(propertyProjectAmountVO.getAccountAmount()==null?"0.00":propertyProjectAmountVO.getAccountAmount());
                    operateThirdPayVO.setAllowedCashWithdrawl(propertyProjectAmountVO.getAllowedCashWithdrawl()==null?"0.00":propertyProjectAmountVO.getAllowedCashWithdrawl());
                }
            }
        }
        page.setRows(list, iOperateThirdPayDao.getThirdPayInfoPageCount(params));
        return page;
    }

    /**
     * <desc>
     *      查询第三方支付信息数量
     * <desc/>
     *
     * @param thirdPartyPayment 第三方支付 1微信支付 2银联支付
     * @return
     * @author Qiang.S
     * @createDate 2018-09-25
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Integer getThirdPayCount(String thirdPartyPayment) throws Exception {
        return iOperateThirdPayDao.getThirdPayCount(thirdPartyPayment);
    }

    /**
     * <desc>
     *      新增第三方支付信息
     * <desc/>
     *
     * @param operateThirdPayInfoDTO 第三方提现信息
     * @return
     * @author Qiang.S
     * @createDate 2018-09-25
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Integer saveThirdPayInfo(OperateThirdPayInfoDTO operateThirdPayInfoDTO) throws Exception {
        ThirdPartyPaymentManagementInfoPO thirdPartyPaymentManagementInfoPO = new ThirdPartyPaymentManagementInfoPO();
        BeanUtils.copyProperties(operateThirdPayInfoDTO, thirdPartyPaymentManagementInfoPO);
        if (operateThirdPayInfoDTO.getPaymentManagementId() ==null || "".equals(operateThirdPayInfoDTO.getPaymentManagementId())) {
            Date createTime = new Date();
            thirdPartyPaymentManagementInfoPO.setPaymentManagementId(LogicIdUtil.bussinessId());
            thirdPartyPaymentManagementInfoPO.setHandlingFee(new BigDecimal(operateThirdPayInfoDTO.getHandlingFee()));
            thirdPartyPaymentManagementInfoPO.setStatus(DataStatusEnum.NORMAL.getType());
            thirdPartyPaymentManagementInfoPO.setCreateTime(createTime);
            thirdPartyPaymentManagementInfoPO.setUpdateTime(null);
            thirdPartyPaymentManagementInfoPO.setLowAmount("2000");
            Map<String, Object> params = SqlUtil.durableData(thirdPartyPaymentManagementInfoPO, PlatformConstants.TABLE_SAVE);
            for(String key : params.keySet()){
                if (params.get(key) != null && "".equals(params.get(key))) {
                    params.put(key,null);
                }
            }
            return iOperateThirdPayDao.saveThirdPayInfo(params);
        }else {
            Date createTime = new Date();
            thirdPartyPaymentManagementInfoPO.setPaymentManagementId(operateThirdPayInfoDTO.getPaymentManagementId());
            thirdPartyPaymentManagementInfoPO.setHandlingFee(new BigDecimal(operateThirdPayInfoDTO.getHandlingFee()));
            thirdPartyPaymentManagementInfoPO.setStatus(Integer.parseInt(operateThirdPayInfoDTO.getStatus()));
            thirdPartyPaymentManagementInfoPO.setCreateTime(createTime);
            thirdPartyPaymentManagementInfoPO.setUpdateTime(null);
            thirdPartyPaymentManagementInfoPO.setLowAmount("2000");
            Map<String, Object> params = SqlUtil.durableData(thirdPartyPaymentManagementInfoPO, PlatformConstants.TABLE_UPDATE);
            for(String key : params.keySet()){
                if (params.get(key) != null && "".equals(params.get(key))) {
                    params.put(key,null);
                }
            }
            return iOperateThirdPayDao.updateThirdPayInfo(params);
        }
    }

    /**
     * <desc>
     *      物业提现规则设置
     * <desc/>
     *
     * @param operateOurPayInfoDTO 公司提现规则
     * @return
     * @author Qiang.S
     * @createDate 2018-09-20
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Integer saveOurPayInfo(OperateOurPayInfoDTO operateOurPayInfoDTO) throws Exception {
        if (iOperateThirdPayDao.getThirdPayCount("3") > 0) {
            return iOperateThirdPayDao.updateOurPayInfo(operateOurPayInfoDTO.getTakeHandlingFee(), operateOurPayInfoDTO.getPayHandlingFee(),
                    operateOurPayInfoDTO.getLowAmount());
        } else {
            ThirdPartyPaymentManagementInfoPO thirdPartyPaymentManagementInfoPO = new ThirdPartyPaymentManagementInfoPO();
            Date createTime = new Date();
            thirdPartyPaymentManagementInfoPO.setPaymentManagementId(LogicIdUtil.bussinessId());
            thirdPartyPaymentManagementInfoPO.setThirdPartyPayment("3");
            thirdPartyPaymentManagementInfoPO.setTakeHandlingFee(operateOurPayInfoDTO.getTakeHandlingFee());
            thirdPartyPaymentManagementInfoPO.setPayHandlingFee(operateOurPayInfoDTO.getPayHandlingFee());
            thirdPartyPaymentManagementInfoPO.setStatus(DataStatusEnum.NORMAL.getType());
            thirdPartyPaymentManagementInfoPO.setCreateTime(createTime);
            thirdPartyPaymentManagementInfoPO.setUpdateTime(null);
            thirdPartyPaymentManagementInfoPO.setLowAmount(operateOurPayInfoDTO.getLowAmount());
            Map<String, Object> params = SqlUtil.durableData(thirdPartyPaymentManagementInfoPO, PlatformConstants.TABLE_SAVE);
            for (String key : params.keySet()) {
                if (params.get(key) != null && "".equals(params.get(key))) {
                    params.put(key, null);
                }
            }
            return iOperateThirdPayDao.saveThirdPayInfo(params);
        }
    }

    /**
     * <desc>
     *      查询第三方支付详细信息
     * <desc/>
     *
     * @param paymentManagementId 支付管理ID 逻辑ID
     * @return
     * @author Qiang.S
     * @createDate 2018-09-25
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public OperateThirdPayVO getThirdPayDetail(String paymentManagementId) throws Exception {
        return iOperateThirdPayDao.getThirdPayDetail(paymentManagementId);
    }

    /**
     * <desc>
     *      获取物业提现规则
     * <desc/>
     *
     * @return
     * @author Qiang.S
     * @createDate 2018-09-25
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public OperateOurThirdPayVO getOurPayInfo() throws Exception {
        return iOperateThirdPayDao.getOurPayInfo();
    }

    /**
     * <desc>
     *      修改状态
     * <desc/>
     *
     * @param paymentManagementId 支付管理ID
     * @param status 支付管理ID  1正常 2禁用
     * @return
     * @author Qiang.S
     * @createDate 2018-09-20
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Integer updateStatus(String paymentManagementId, String status) throws Exception {
        return iOperateThirdPayDao.updateStatus(paymentManagementId,status);
    }
}
